import router from './router'
import {getUserInfo} from './api/login'
import store from './store'

router.beforeEach((to, from, next) => {
    // 获取token值
    const token = localStorage.getItem('mxg-msm-token')
    // 判断没有token
    if(!token){
        // 没有token情况下想跳转非login路由强制跳转登录页面
        if(to.path !== '/login'){
            next({path: '/login'})
        }else{
            next()
        }
    }else{
        if(to.path == '/login'){
            next()
        }else{
            // 获取存储数据中的用户信息
            const userInfo = store.state.user.user
            // 判断有无用户信息
            if(userInfo){
                next()
            }else{
                // 通过token获取用户信息
                store.dispatch('GetUserInfo').then(response => {
                    if(response.flag){
                        next()
                    }else{
                        next({path: '/login'})
                    }
                }).catch(error => {

                })
            }
        }
    }

})